package com.hnkywl.sts.service.zjgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.htgl.CghtDao;
import com.hnkywl.sts.dao.sfgl.SkshDao;
import com.hnkywl.sts.dao.zjgl.JzjsDao;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.organ.Gc;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.zjgl.Bzj;
import com.hnkywl.sts.entity.zjgl.Hthk;
import com.hnkywl.sts.entity.zjgl.Jzjs;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.CalcUtils;
import com.hnkywl.sts.util.DateUtil;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by HL on 2016/4/12.
 */
@Service
public class JzjsService {

    private JzjsDao jzjsDao;
    private SkshDao skshDao;
    private CghtDao cghtDao;


    @Transactional
    public String findAlljzjs(Long gsId, String khMc, String hth, int page, int rows) {
        Page<Jzjs> jzjsPage = this.jzjsDao.findJzjsList(gsId, khMc, hth, page, rows);
        List<Jzjs> jzjsList = jzjsPage.getResult() == null ? new ArrayList<Jzjs>() : jzjsPage.getResult();
        String bzjJson = BaseUtils.fetchCollectionToJson(jzjsList, "id", "nbgs.jc", "cght.htbm", "cght.hsze", "kh.qc", "fklx", "khfksj", "sk.bm", "je", "ye", "txl", "endDate", "tx", "txye", "mxts", "jsts", "zt");
        return "{\"total\":" + jzjsPage.getTotalCount() + ",\"rows\":[" + bzjJson + "]}";
    }


    @Transactional
    public void saveJzjs(Jzjs jzjs) {
        jzjs.setDateCreated(new Date());
        jzjs.setCreateUser(YgService.getCurrentYg());
        jzjs.setCreateOrgan(YgService.getCurrentYg().getZggs());
        jzjs.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        jzjs.setLastUpdated(jzjs.getDateCreated());
        Sfk sk = this.skshDao.get(jzjs.getSk().getId());
        sk.setZt("ysd");
        jzjs.setJe(sk.getYe());
        jzjs.setZt("2");
        jzjs.setKhfksj(sk.getKhfksj());
        jzjs.setFklx(sk.getFkfs().getZ().equals("承兑") ? "1" : "0");
        jzjs.setKxsy(sk.getKxsy());
        jzjs.setFkfs(sk.getFkfs());
        if (sk.getFkfs().getZ().equals("承兑")) {
           // if (sk.getSfjstx().equals("0")) {
                Cght cght = this.cghtDao.get(jzjs.getCght().getId());
                long calDays;
                long reduceDays = DateUtil.getDay(sk.getEndDate(), sk.getKhfksj());
                if(cght.getMxts()==null){
                    throw new RuntimeException("采购合同信息中免息天数不能为空请修改添加！");
                }else{
                    calDays = reduceDays - cght.getMxts();
                }
                Double tx = 0.0;
                if (calDays > 0) {
                    tx = CalcUtils.round(((sk.getYe() * cght.getTxl() / 100) / 30.0) * calDays, 2);
                }
                jzjs.setTx(tx);
                jzjs.setTxye(tx);
                jzjs.setMxts(cght.getMxts());
                jzjs.setEndDate(sk.getEndDate());
                jzjs.setJsts(calDays);
                jzjs.setTxl(CalcUtils.round(cght.getTxl(), 4));
                jzjs.setYe(CalcUtils.round((sk.getYe() - tx),4));
               // jzjs.setYe(sk.getYe() - tx);
                sk.setSfjstx("1");
           /* } else {
                jzjs.setTx(sk.getTx());
                jzjs.setYe(sk.getYe());
            }*/
        } else {
            jzjs.setYe(sk.getYe());
        }
        this.skshDao.save(sk);
        this.jzjsDao.save(jzjs);
    }

    public List<Jzjs> findJzjsByCght(Cght cght) {
        return this.jzjsDao.findByProperty("cght", cght, null);
    }




    @Transactional
    public void tjJzjs(Long jzjsid) {
        Jzjs jzjs = this.jzjsDao.get(jzjsid);
        jzjs.setZt("0");
        this.jzjsDao.save(jzjs);
     /*   Sfk sfk = this.skshDao.get(jzjs.getSk().getId());
        sfk.setZt("ytz");
        if (jzjs.getFkfs().getZ() == "承兑") {
            sfk.setTx(CalcUtils.round(jzjs.getTx(), 2));
            sfk.setTxl(jzjs.getTxl());
            sfk.setMxts(jzjs.getMxts());
            sfk.setJstx(jzjs.getJsts());
            sfk.setTxye(jzjs.getTxye());
        }*/
        //sfk.setYe(CalcUtils.round(jzjs.getYe(), 2));
       // sfk.setSfje(CalcUtils.round(sfk.getSfje(),2)-CalcUtils.round(jzjs.getYe(),2));
        //this.skshDao.save(sfk);
    }

    @Transactional
    public void deletejzjs(Long id) {
        Jzjs jzjs = jzjsDao.get(id);
        Sfk sk = this.skshDao.get(jzjs.getSk().getId());
        sk.setZt("ytz");
        jzjs.setLastUpdated(new Date());
        jzjs.setDeleted(1);
        this.skshDao.save(sk);
        this.jzjsDao.save(jzjs);
    }

    /*   public String findAlljzjs(String htbm,Integer pageNo, Integer pageSize){
        Page<Map> page = jzjsDao.findjzjsList(htbm, pageNo, pageSize);

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
        *//*Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);*//*
        // return "{\"rows\":" + json + "}";
    }*/
    @Autowired
    public void setJzjsDao(JzjsDao jzjsDao) {
        this.jzjsDao = jzjsDao;
    }
    @Autowired
    public void setSkshDao(SkshDao skshDao) {
        this.skshDao = skshDao;
    }
    @Autowired
    public void setCghtDao(CghtDao cghtDao) {
        this.cghtDao = cghtDao;
    }

}

